<?php

/**
 * @fileoverview    美啦用户修改密码
 * @author          zhanglei11
 * @date            Thu Jan 05 14:34:25 CST 2012
 * @copyright       sina meila
 */

include_once '../../__global.php';
include_once SERVER_ROOT_PATH.'/include/ml_function_lib.php';

class aj_addlike extends ml_controller{
    
    private $pid ;
    private $aid;
    private $cid;
    private $usid;
    private $type;
    private $callback;
    private $isfirst;
    private $_uid;
    private $_oMeilaPic;
    

    function initParam() {
        $this->pid = (int)$this->input('pid','G');                // 图片id
        $this->aid = (int)$this->input('aid','G');                    // 相册id
        $this->cid = (int)$this->input('cid','G');                    // 类型id
        $this->usid = $this->input('uid','G');                    // uid
        $this->type = $this->input('type','G');                    // 类型
        $this->isfirst = (int)$this->input('isfirst', 'G', 0);
        $this->callback = $this->input('callback','G');
        // 获取当前登录用户 uid
        $this->_uid = $this->__visitor['uid'];
    }
    
    function checkParam(){
        $this->check_referer();    
    }
    function main() {

        if(!is_null($_COOKIE['LM_UNLOGIN_LIKE'])) {
            $likeids =  explode(',',$_COOKIE['LM_UNLOGIN_LIKE']);
           if(!in_array($this->pid,$likeids)) {  //  如果不在cookie里 
               $likeids[] = $this->pid;
               //    双写2个域
               setcookie('LM_UNLOGIN_LIKE',implode(',',$likeids) ,time()+86400,'/','.sina.com.cn');
               setcookie('LM_UNLOGIN_LIKE',implode(',',$likeids) ,time()+86400,'/','meila.com');       
           }else {
                   if(!empty($this->callback)) {
                       echo 'try{'.$this->callback.'('. json_encode(array('code'=>ML_RCODE_FAIL,'msg'=>'重复喜欢')).');}catch(e){}';
                       exit;
                   }else{
                       $this->api_output(ML_RCODE_FAIL,array(),'重复喜欢'); 
                   }
           }
        }else {
            setcookie('LM_UNLOGIN_LIKE',$this->pid,time()+86400,'/','.sina.com.cn');             
            setcookie('LM_UNLOGIN_LIKE',$this->pid,time()+86400,'/','meila.com');
        }
         
        $this->_oMeilaPic = new ml_model_admin_dbMeilaPic();
             
        
        if(empty($this->_uid))
            $set = 'like_visitor';
        else
            $set = 'like_users';
        
        $rs = $this->_oMeilaPic->incr_like_cnt($set, $this->pid);
        
        
        if($rs) {
            $like_cnt = $this->_get_like_cnt();
            if(!empty($this->callback)) {
                echo 'try{'.$this->callback.'('.json_encode(array('code'=>ML_RCODE_SUCC,'data'=>array('likeCnt'=>$like_cnt))) .');}catch(e){}';
                exit;
            }else{
                echo json_encode(array('code'=>ML_RCODE_SUCC,'data'=>array('likeCnt'=>$like_cnt))) ;
            }
            
        }else {   
            if(!empty($this->callback)) {
                echo 'try{'.$this->callback.'('. json_encode(array('code'=>ML_RCODE_FAIL)).');}catch(e){}';
                exit;
            }else{
                echo json_encode(array('code'=>ML_RCODE_FAIL)) ;
            }
        }
    }
    public function _get_like_cnt(){
        $oRdsAlbumCount = new ml_model_album_rdsCount();
        $oRdsAlbumCount->incrAlbumLikeCnt($this->aid);
        if ($this->isfirst) {
            $likeCnt = (int)$oRdsAlbumCount->getAlbumLikeCnt($this->aid);
            return $likeCnt;
        }
        
        $rs = $this->_oMeilaPic->getPicByPid($this->pid);
        if(!$rs)
            return false;
        
        $pic = $this->_oMeilaPic->get_data();
        $like_cnt = (int)$pic['like_visitor'] + (int)$pic['like_user'];
        
        $oMeilaPoint = new ml_model_admin_dbPoint();
        $rs = $oMeilaPoint->get_by_pointid(array($this->pid));
        $points = $oMeilaPoint->get_data();
        $rids = array();
        if (empty($points))
            return gb_function_lib::getVitrulCnt($this->pid, $like_cnt);
        
        foreach ($points as $point) {
            $pinfo = json_decode($point['point_info'], true);
            if (!empty($pinfo['content_id'])){
                $rids[] = $pinfo['content_id'];
            }
        }
            
        if(empty($rids))
            return gb_function_lib::getVitrulCnt($this->pid, $like_cnt);
        $sum = 0;
        
        $oRedisC = new ml_model_rdsCount();
        $rs = $oRedisC->getAttitudeCount($rids);
        if(!$rs)
            return false;
        $Ac = $oRedisC->get_data();
        foreach ($Ac as $count) {
            $sum += (int)$count;
        }
        
        return (int)$sum + gb_function_lib::getVitrulCnt($this->pid, $like_cnt);
    }

}

new aj_addlike();

